var baseConfig = require("../../baseConfig");
Page({
  data: {

  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      appId: options.appId,
      appName: options.appName,
      date: options.date,
      month: options.date.substring(0, options.date.length - 3),
    })
    // console.log(this.data.month);
    this.getDate()
    this.get_today_revenue()
  },
  toProfitDetails(e) { // 前往profitDetails页面
    console.log('profitDetails', e.currentTarget.dataset);
    let {
      icon,
      name,
      id,
      date,
    } = e.currentTarget.dataset;
    // console.log(icon, name, id, this.data.appName);
    wx.navigateTo({
      url: '/pages/profitDetails/profitDetails?appName=' + this.data.appName +
        '&icon=' + icon +
        '&name=' + name +
        '&date=' + date +
        '&appId=' + this.data.appId +
        '&id=' + id
    })
  },
  get_today_revenue() { // 获取选中日期的数据
    let uid = wx.getStorageSync('userid')
    wx.request({
      url: baseConfig.baseUrl + 'get_today_revenue',
      method: "GET",
      data: {
        uid: uid,
        date: this.data.date,
        appId: this.data.appId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: res => {
        this.isToday()
        console.log('获取选中日期的数据', res.data.data);
        if (res.data.data == '') {
          // console.log('当日无数据');
          this.setData({
            kong: true
          })
        } else {
          this.setData({
            kong: false
          })
        }
        this.setData({
          list: res.data.data,
        })
      }
    })
  },
  bindDateChange: function (e) { // 点击选择日期
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      month: e.detail.value.substring(0, e.detail.value.length - 3),
    })
    this.get_today_revenue()
  },
  getDate() { // 获取当日年月日
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var Y = date.getFullYear(); // 获取年 
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); // 获取月
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); // 获取日
    const yearMonthDay = Y + '-' + M + '-' + D;
    this.setData({
      yearMonthDay
    })
    this.isToday()
  },
  isToday() { // 判断是否为本日数据
    if (this.data.yearMonthDay == this.data.date) {
      // console.log('本日数据');
      this.setData({
        today: true
      })
    } else {
      // console.log('非本日数据');
      this.setData({
        today: false
      })
    }
  },
  onReachBottom: function () { // 页面上拉触底事件的处理函数
    console.log('onReachBottom');
  },
})